#!/bin/bash

# install binary files into this directory
PREFIX="/tools/"
# uncompress files into this derectory
SOURCES="${BUILD}/decompress_src"
mkdir  -pv ${SOURCES}

function echo_exit(){
    if [ $# -ge 1 ]; then 
        CMD="$*"
        if [ -z "${PKG}" ]; then
            echo "Failed to run [${CMD}]"
        else
            echo "Failed to install ${PKG} when run [${CMD}]"
        fi
    fi
    exit 1
}

function run_cmd(){
    [[ $# -lt 1 ]] && return
    eval "$@" || echo_exit "$@"
}

function get_uncompress_cmd(){
    f_path="${1}"
    [ -z "${f_path}" ] &&  return
    f_name=`basename ${f_path}`

    restore=1
    shopt -q nocasematch
    if [ $? -eq 1 ]; then
        shopt -s nocasematch
        restore=0
    fi
    shopt -s nocasematch
    if [[ "${f_name}" == *.zip ]]; then
        cmd="unzip ${f_path}"
    elif [[ "${f_name}" == *.bz2 ]]; then
        cmd="tar jxvf ${f_path}"
    elif [[ "${f_name}" == *.tgz ]]; then
        cmd="tar zxvf ${f_path}"
    elif [[ "${f_name}" == *.tar.gz ]]; then
        cmd="tar zxvf ${f_path}"
    elif [[ "${f_name}" == *.gz ]]; then
        cmd="gzip -d ${f_path}"
    fi
    
    if [ ${restore} -eq 0 ]; then
        shopt -u nocasematch
    fi
    echo "${cmd}"
}

function decompress_and_stepinto(){
    # decompress a compressed file under a temporary in current directory,
    # after decompressed, mv the newly generated directory to current directory,
    # and go into the newly generated directory when decomprocession 
    # and delete the temporary directory finally 
    f_path="${1}"
    [ -z "${f_path}" ] && return
    [ ! -f "${f_path}" ] && return
    
    f_name=`basename ${f_path}`
    d_temp=`mktemp -d ${f_name}.XXX`
    cd ${d_temp}
    uncompress_cmd=`get_uncompress_cmd ${f_path}`
    echo ${uncompress_cmd}
    ${uncompress_cmd} >& uncompress.log
    if [ $? -ne 0 ]; then
        echo "Faile to uncompress file ${f_path}"
        echo "You can reference the log file ${d_temp}/uncompress.log"
        exit 1
    fi
    rm uncompress.log
    dname_uncompress=`ls`
    mv "${dname_uncompress}" ../
    cd ../
    rmdir ${d_temp}
    cd ${dname_uncompress}
}

function uncompress_patch(){
    [[ -n "$1" ]] || return
    f_pkg=$1 && shift
    f_patch="$@"

    cd ${SOURCES}
    decompress_and_stepinto "${SRC}/${f_pkg}"
   
    if [ -n "${f_patch}" ]; then
        for patch in ${f_patch}; do
            patch -Np1 -i "${SRC}/${patch}"
        done
    fi
}

function install_tcl(){
    PKG='Tcl'

    uncompress_patch tcl8.5.8-src.tar.gz

    cd unix
    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd TZ=UTC make test
    run_cmd make install
    chmod -v u+w ${PREFIX}/lib/libtcl8.5.so
    run_cmd make install-private-headers
    ln -sv tclsh8.5 ${PREFIX}/bin/tclsh
}

function install_expect(){
    PKG='Except'

    uncompress_patch expect-5.44.1.15.tar.bz2 expect-5.44.1.15-no_tk-1.patch 

    cp -v configure{,.orig}
    sed 's:/usr/local/bin:/bin:' configure.orig >configure
    run_cmd ./configure --prefix=${PREFIX} --with-tcl=${PREFIX}/lib --with-tclinclude=${PREFIX}/include --with-tk=no
    run_cmd make
    run_cmd make test
    run_cmd make 'SCRIPTS=""' install
}

function install_DeijaGNU(){
    PKG='DejaGNU'
    
    uncompress_patch dejagnu-1.4.4.tar.gz dejagnu-1.4.4-consolidated-1.patch

    run_cmd ./configure --prefix=${PREFIX}
    #run_cmd make
    run_cmd make install
    run_cmd make check
}

function install_Ncurses(){
    PKG='Ncurses'
    
    uncompress_patch ncurses-5.7.tar.gz

    run_cmd ./configure --prefix=${PREFIX} --with-shared --without-debug --without-ada --enable-overwrite
    run_cmd make
    run_cmd make install
}

function install_Bash(){
    PKG='Bash'
    
    uncompress_patch bash-4.1.tar.gz bash-4.1-fixes-2.patch 

    run_cmd ./configure --prefix=${PREFIX} --without-bash-malloc
    run_cmd make
    run_cmd make tests
    run_cmd make install
    ln -vs bash /tools/bin/sh
}

function install_Bzip2(){
    PKG='Bzip2'
    
    uncompress_patch bzip2-1.0.5.tar.gz 

    run_cmd make
    run_cmd make PREFIX=${PREFIX} install
}

function install_Coreutils(){
    PKG='Coreutils'
    
    uncompress_patch coreutils-8.5.tar.gz

    run_cmd ./configure --prefix=${PREFIX} --enable-install-program=hostname
    run_cmd make
    run_cmd make RUN_EXPENSIVE_TESTS=yes check 
    run_cmd make install
    cp -v src/su ${PREFIX}/bin/su-tools
}

function install_Diffutils(){
    PKG='Diffutils'
    
    uncompress_patch diffutils-3.0.tar.gz

    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check 
    run_cmd make install
}

function install_File(){
    PKG='File'
    
    uncompress_patch file-5.04.tar.gz 

    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check 
    run_cmd make install
}

function install_Findutils(){
    PKG='Findutils'
    
    uncompress_patch findutils-4.4.2.tar.gz

    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check 
    run_cmd make install
}

function install_Gawk(){
    PKG='Gawk'
    
    uncompress_patch gawk-3.1.8.tar.bz2

    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check 
    run_cmd make install
}

function install_Gettext(){
    PKG='Gettext'
    
    uncompress_patch gettext-0.18.1.1.tar.gz 

    cd gettext-tools
    run_cmd ./configure --prefix=${PREFIX} --disable-shared
    run_cmd make -C gnulib-lib
    run_cmd make -C src msgfmt
    cp -v src/msgfmt ${PREFIX}/bin
}

function install_Grep(){
    PKG='Grep'
    
    uncompress_patch grep-2.6.3.tar.gz

    run_cmd ./configure --prefix=${PREFIX} --disable-perl-regexp
    run_cmd make
    run_cmd make check
    run_cmd make install
}

function install_Gzip(){
    PKG='Gzip'
    
    uncompress_patch gzip-1.4.tar.gz

    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check
    run_cmd make install
}

function install_M4(){
    PKG='M4'
    
    uncompress_patch m4-1.4.14.tar.bz2

    sed -i -e '/"m4.h"/a#include <sys/stat.h>' src/path.c
    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check
    run_cmd make install
}

function install_Make(){
    PKG='Make'
    
    uncompress_patch make-3.82.tar.bz2

    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check
    run_cmd make install
}

function install_Patch(){
    PKG='Patch'
    
    uncompress_patch patch-2.6.1.tar.bz2

    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check
    run_cmd make install
}

function install_Perl(){
    PKG='Perl'
    
    uncompress_patch perl-5.12.1.tar.bz2 perl-5.12.1-libc-1.patch

    run_cmd sh Configure -des -Dprefix=${PREFIX} -Dstatic_ext='Data/Dumper Fcntl IO'
    run_cmd make perl utilities ext/Errno/pm_to_blib
    cp -v perl pod/pod2man ${PREFIX}/bin
    mkdir -pv ${PREFIX}/lib/perl5/5.12.1 
    cp -Rv lib/* ${PREFIX}/lib/perl5/5.12.1
}

function install_Sed(){
    PKG='Sed'
    
    uncompress_patch sed-4.2.1.tar.bz2

    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check
    run_cmd make install
}

function install_Tar(){
    PKG='Tar'
    
    uncompress_patch tar-1.23.tar.bz2

    sed -i /SIGPIPE/d src/tar.c
    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check
    run_cmd make install
}

function install_Texinfo(){
    PKG='Tar'
    
    uncompress_patch texinfo-4.13a.tar.gz

    run_cmd ./configure --prefix=${PREFIX}
    run_cmd make
    run_cmd make check
    run_cmd make install
}

function stripping(){
    strip --strip-debug ${PREFIX}/lib/*
    strip --strip-unneeded ${PREFIX}/{,s}bin/*
    rm -rf ${PREFIX}/{,share}/{info,man}
}

function main(){

    install_tcl
    install_expect
    install_DeijaGNU
    install_Ncurses
    install_Bash
    install_Bzip2
    install_Coreutils
    install_Diffutils
    install_File
    install_Findutils
    install_Gawk
    install_Gettext
    install_Grep
    install_Gzip
    install_M4
    install_Make
    install_Patch
    install_Perl
    install_Sed
    install_Tar
    install_Texinfo
    
#    stripping
}

main "$@"
